package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.HistoryKeywordVO;
import com.cskaoyan.bean.vo.WxKeyWordsVO;
import com.cskaoyan.mapper.MallGoodsMapper;
import com.cskaoyan.mapper.MallKeywordMapper;
import com.cskaoyan.mapper.MallSearchHistoryMapper;
import com.cskaoyan.service.WxSearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Chunlai
 * @description:
 * @time: 2021/7/12 22:12
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    MallSearchHistoryMapper mallSearchHistoryMapper;

    @Autowired
    MallKeywordMapper mallKeywordMapper ;

    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Override
    public WxKeyWordsVO querymessages() {

        Subject subject = SecurityUtils.getSubject();
        MallUser mallUser = (MallUser) subject.getPrincipal();

        Integer id = mallUser.getId();


        List<HistoryKeywordVO> historyKeywordVOList = new ArrayList<>();    // historyKeywordList

        if (id != null){
            //todo 1 历史
            MallSearchHistoryExample mallSearchHistoryExample = new MallSearchHistoryExample();
            MallSearchHistoryExample.Criteria criteria = mallSearchHistoryExample.createCriteria();
            criteria.andUserIdEqualTo(id);
            criteria.andDeletedEqualTo(false);
            List<MallSearchHistory> mallSearchHistoryList = mallSearchHistoryMapper.selectByExample(mallSearchHistoryExample);

            for (MallSearchHistory mallSearchHistory : mallSearchHistoryList) {
                String keyword = mallSearchHistory.getKeyword();
                HistoryKeywordVO historyKeywordVO = new HistoryKeywordVO(keyword);
                historyKeywordVOList.add(historyKeywordVO);
            }
            //todo 2  热度
            MallKeywordExample mallKeywordExample = new MallKeywordExample();
            MallKeywordExample.Criteria criteria1 = mallKeywordExample.createCriteria();
            criteria1.andIsHotEqualTo(true);
            List<MallKeyword> mallKeywords = mallKeywordMapper.selectByExample(mallKeywordExample);

            //todo 3 默认
            Integer isdefault = 1 ;
            MallKeyword mallKeyword = mallKeywordMapper.selectBydefault(isdefault);

            WxKeyWordsVO wxKeyWordsVO = new WxKeyWordsVO(mallKeyword,mallKeywords,historyKeywordVOList);

            return wxKeyWordsVO;
        }else{
            //todo 2  热度
            MallKeywordExample mallKeywordExample = new MallKeywordExample();
            MallKeywordExample.Criteria criteria1 = mallKeywordExample.createCriteria();
            criteria1.andIsHotEqualTo(true);
            List<MallKeyword> mallKeywords = mallKeywordMapper.selectByExample(mallKeywordExample);

            //todo 3 默认
            Integer isdefault = 1 ;
            MallKeyword mallKeyword = mallKeywordMapper.selectBydefault(isdefault);


            List<HistoryKeywordVO> keywordVOList = new ArrayList<>();
            WxKeyWordsVO wxKeyWordsVO = new WxKeyWordsVO(mallKeyword,mallKeywords,keywordVOList);

            return wxKeyWordsVO;
        }



    }

    @Override
    public void updatehistorystatus() {
        Subject subject = SecurityUtils.getSubject();
        MallUser mallUser = (MallUser) subject.getPrincipal();
        Integer id = mallUser.getId();

        MallSearchHistoryExample mallSearchHistoryExample = new MallSearchHistoryExample();
        MallSearchHistoryExample.Criteria criteria = mallSearchHistoryExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MallSearchHistory> mallSearchHistoryList = mallSearchHistoryMapper.selectByExample(mallSearchHistoryExample);

        for (MallSearchHistory mallSearchHistory : mallSearchHistoryList) {
            Integer idHistory = mallSearchHistory.getId();
            mallSearchHistoryMapper.updatebyIdHistory(idHistory);
        }

    }

    @Override
    public List<String> helper(String keyword) {
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria criteria = mallGoodsExample.createCriteria();
        criteria.andKeywordsLike("%"+ keyword +"%");
        List<MallGoods> mallGoods = mallGoodsMapper.selectByExample(mallGoodsExample);
        ArrayList<String> goods_list = new ArrayList<>();
        for (MallGoods mallGood : mallGoods) {
            String name = mallGood.getName();
            goods_list.add(name);
        }
        return goods_list;
    }

}